package dao;

//晚归记录


import bean.Laterecord;
import util.DriverManegerUitl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class LaterecordDao {
    private Connection conn;
    private PreparedStatement pre;
    private ResultSet resultSet;


    /**
     * 所有学生离校信息
     *
     * @return
     */
    //得到所有学生离校信息
    public ArrayList<Laterecord> getAllLaterecord() {
        conn = DriverManegerUitl.getConnection();
        ArrayList<Laterecord> list = new ArrayList<>();

        try {
            pre = conn.prepareStatement("select * from laterecord");
            resultSet = pre.executeQuery();
            while (resultSet.next()) {
                Laterecord laterecord = new Laterecord();
                laterecord.setStudentname(resultSet.getString("studentname"));
                laterecord.setDormitoryid(resultSet.getString("dormitoryid"));
                laterecord.setLatetime(resultSet.getDate("latetime"));
                laterecord.setReason(resultSet.getString("reason"));
                list.add(laterecord);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
        return null;
    }

    /**
     * 学生端
     * 根据学生姓名看信息
     *
     * @param studentname
     * @return
     */
    //学生看本人的离校信息 根据学生姓名看信息
    public ArrayList<Laterecord> getLaterecordByStudentname(String studentname) {
        ArrayList<Laterecord> list = new ArrayList<>();
        conn = DriverManegerUitl.getConnection();
        try {
            pre = conn.prepareStatement("select * from laterecord where studentname=?");
            pre.setString(1, studentname);
            resultSet = pre.executeQuery();
            while (resultSet.next()) {
                Laterecord laterecord = new Laterecord();
                laterecord.setStudentname(resultSet.getString("studentname"));
                laterecord.setDormitoryid(resultSet.getString("dormitoryid"));
                laterecord.setLatetime(resultSet.getDate("latetime"));
                laterecord.setReason(resultSet.getString("reason"));
                list.add(laterecord);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
        return null;
    }

    /**
     * 管理员提交晚归信息
     * @param laterecord
     */
//管理员提交晚归信息
    public void submitInfo(Laterecord laterecord){
        conn=DriverManegerUitl.getConnection();
        try {
            pre=conn.prepareStatement("insert into laterecord (studentname,dormitoryid,latetime,reason) values (?,?,?,?);");
            pre.setString(1,laterecord.getStudentname());
            pre.setString(2,laterecord.getDormitoryid());
            pre.setDate(3,new java.sql.Date(laterecord.getLatetime().getTime()));
            pre.setString(4,laterecord.getReason());

            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
    }

    //管理员根据宿舍号，学生姓名修改信息
    public void changeInfoByDormidAndStuname(Laterecord laterecord){
        conn=DriverManegerUitl.getConnection();
        try {
            pre=conn.prepareStatement("update laterecord set latetime=?,reason=? where studentname=? and dormitoryid=?");
            pre.setDate(1,new java.sql.Date(laterecord.getLatetime().getTime()));
            pre.setString(2,laterecord.getReason());
            pre.setString(3,laterecord.getStudentname());
            pre.setString(4,laterecord.getDormitoryid());
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
    }
    //管理员删除
//    根据用户名删除信息
    public  void delInfoBySutdentname(String name){
        conn=DriverManegerUitl.getConnection();
        try {
            pre=conn.prepareStatement("delete from laterecord where studentname=?");
            pre.setString(1,name);
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn,pre,resultSet);
        }
    }
}
